package com.diligent.cloud.sfc.service.impl;

import com.alibaba.fastjson.JSON;
import com.canny.commons.model.Codes;
import com.canny.commons.model.ListResult;
import com.canny.commons.model.ObjectResult;
import com.canny.commons.page.IPage;
import com.canny.commons.page.Pagination;
import com.diligent.cloud.logistics.container.enums.BoxFillDocStatus;
import com.diligent.cloud.logistics.container.enums.BoxStatus;
import com.diligent.cloud.logistics.container.enums.DocOperation;
import com.diligent.cloud.logistics.container.model.LcmBoxFillDetail;
import com.diligent.cloud.logistics.container.model.LcmBoxFillMaster;
import com.diligent.cloud.logistics.container.model.LcmBoxLedger;
import com.diligent.cloud.logistics.container.service.LcmBoxFillDetailFeignService;
import com.diligent.cloud.logistics.container.service.LcmBoxFillFeignService;
import com.diligent.cloud.logistics.container.service.LcmBoxFillMasterFeignService;
import com.diligent.cloud.logistics.container.service.LcmBoxLedgerFeignService;
import com.diligent.cloud.primary.data.feign.service.mdm.MdmBaseFeignService;
import com.diligent.cloud.primary.data.model.Result;
import com.diligent.cloud.primary.data.model.rt.RtWorkCenter;
import com.diligent.cloud.prodbatch.model.MfgProdBatch;
import com.diligent.cloud.prodbatch.model.MfgProdBatchResp;
import com.diligent.cloud.prodbatch.service.MfgProdBatchFeignService;
import com.diligent.cloud.sfc.config.OauthConfig;
import com.diligent.cloud.sfc.model.MatType;
import com.diligent.cloud.sfc.model.MfgConsume;
import com.diligent.cloud.sfc.model.enums.MatStatus;
import com.diligent.cloud.sfc.model.info.BoxInfo;
import com.diligent.cloud.sfc.model.info.MatStock;
import com.diligent.cloud.sfc.model.info.inventory.AluminumStickInventoryInfo;
import com.diligent.cloud.sfc.model.params.inventory.AluminumStickInventoryParams;
import com.diligent.cloud.sfc.service.ISfcService;
import com.diligent.cloud.sfc.service.WorkShopExtrusionService;
import com.diligent.cloud.sfc.util.ConverterUtil;
import com.diligent.cloud.web.model.PageAndCondition;
import com.diligent.cloud.workorder.model.WoOperationCompleteDetail;
import com.diligent.cloud.workorder.model.WoOrder;
import com.diligent.cloud.workorder.model.WoOrderCompleteDetail;
import com.diligent.cloud.workorder.service.WoOperationCompleteDetailFeignService;
import com.diligent.cloud.workorder.service.WoOrderCompleteDetailFeignService;
import com.diligent.cloud.workorder.service.WoOrderFeignService;
import com.diligent.cloud.workordermd.feign.service.MfgConsumeDetailFeignService;
import com.diligent.cloud.workordermd.feign.service.MfgConsumeMasterFeignService;
import com.diligent.cloud.workordermd.feign.service.MfgMdApplyUseFeignService;
import com.diligent.cloud.workordermd.model.MfgConsumeDetail;
import com.diligent.cloud.workordermd.model.MfgConsumeMaster;
import com.diligent.cloud.workordermd.model.MfgMdApplyUse;
import com.diligent.cloud.workordermd.model.MfgUserEnvironment;
import com.diligent.foundation.data.model.v2.Condition;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.security.Principal;
import java.util.*;

/**
 * WorkShopExtrusionServiceImpl class
 *
 * @author xf107278
 * @date 4/24/2020
 */
@Service
public class WorkShopExtrusionServiceImpl implements WorkShopExtrusionService {
    private ISfcService sfcService;
    private MfgConsumeMasterFeignService mfgConsumeMasterFeignService;
    private MfgConsumeDetailFeignService mfgConsumeDetailFeignService;
    private WoOrderFeignService orderFeignService;
    private LcmBoxLedgerFeignService lcmBoxLedgerFeignService;
    private LcmBoxFillDetailFeignService lcmBoxFillDetailFeignService;
    private MfgProdBatchFeignService prodBatchFeignService;
    private MfgMdApplyUseFeignService mfgMdApplyUseFeignService;
    private LcmBoxFillFeignService lcmBoxFillFeignService;
    private LcmBoxFillMasterFeignService lcmBoxFillMasterFeignService;
    private final WoOperationCompleteDetailFeignService operationCompleteDetailFeignService;
    private final OauthConfig.RequestParams requestParams;
    private final MdmBaseFeignService mdmBaseFeignService;



    public WorkShopExtrusionServiceImpl(ISfcService sfcService,
                                        MfgConsumeMasterFeignService mfgConsumeMasterFeignService,
                                        MfgConsumeDetailFeignService mfgConsumeDetailFeignService,
                                        WoOrderFeignService orderFeignService,
                                        LcmBoxLedgerFeignService lcmBoxLedgerFeignService,
                                        LcmBoxFillDetailFeignService lcmBoxFillDetailFeignService,
                                        MfgProdBatchFeignService prodBatchFeignService,
                                        MfgMdApplyUseFeignService mfgMdApplyUseFeignService,
                                        LcmBoxFillFeignService lcmBoxFillFeignService,
                                        LcmBoxFillMasterFeignService lcmBoxFillMasterFeignService,
                                        WoOperationCompleteDetailFeignService operationCompleteDetailFeignService,
                                        OauthConfig.RequestParams requestParams,
                                        MdmBaseFeignService mdmBaseFeignService) {
        this.sfcService = sfcService;
        this.mfgConsumeMasterFeignService = mfgConsumeMasterFeignService;
        this.mfgConsumeDetailFeignService = mfgConsumeDetailFeignService;
        this.orderFeignService = orderFeignService;
        this.lcmBoxLedgerFeignService = lcmBoxLedgerFeignService;
        this.lcmBoxFillDetailFeignService = lcmBoxFillDetailFeignService;
        this.prodBatchFeignService = prodBatchFeignService;
        this.mfgMdApplyUseFeignService = mfgMdApplyUseFeignService;
        this.lcmBoxFillFeignService = lcmBoxFillFeignService;
        this.lcmBoxFillMasterFeignService = lcmBoxFillMasterFeignService;
        this.operationCompleteDetailFeignService = operationCompleteDetailFeignService;
        this.requestParams = requestParams;
        this.mdmBaseFeignService = mdmBaseFeignService;

    }

    @Override
    public ObjectResult addAluminumStickInventory(AluminumStickInventoryParams aluminumStickInventoryParams,
                                                  Principal principal) {

        for (AluminumStickInventoryInfo aluminumStickInventoryInfo :
                aluminumStickInventoryParams.getAluminumStickInventoryInfos()) {

            MfgConsume mfgConsume = sfcService.addMatInventoryInfo(aluminumStickInventoryInfo,
                    sfcService.addMatConsumerInventory(aluminumStickInventoryParams, principal));

            mfgConsume.getMfgConsumeMaster().setDocStatus(0);
            mfgConsume.getMfgConsumeMaster().setMatCode(aluminumStickInventoryParams.getDocItem().toString());
            mfgConsume.getMfgConsumeMaster().setMatType(aluminumStickInventoryParams.getMaterial());
            mfgConsume.getMfgConsumeMaster().setMatName(aluminumStickInventoryParams.getDocItemName());

            mfgConsume.getMfgConsumeDetail().setDocItem(aluminumStickInventoryParams.getDocItem());
            mfgConsume.getMfgConsumeDetail().setLotCode(aluminumStickInventoryInfo.getLotCode());

            com.diligent.cloud.workordermd.model.MfgConsume mfgConsume1 = new com.diligent.cloud.workordermd.model.MfgConsume();
            BeanUtils.copyProperties(mfgConsume,mfgConsume1);
            mfgConsumeMasterFeignService.add(mfgConsume1);

//            mfgConsumeMasterFeignService.insert(mfgConsume.getMfgConsumeMaster());
//            mfgConsumeDetailFeignService.insert(mfgConsume.getMfgConsumeDetail());
        }
        return new ObjectResult("");
    }

    @Override
    public ListResult boxReportedFilter(String machCode, String orderId, Boolean isHanging) {
        List<BoxInfo> answer = new ArrayList<>();
        BoxInfo boxInfo;
        Boolean byOrder = orderId == null ? false : !"null".equalsIgnoreCase(orderId) ? true : false;
        Boolean byMachCode = machCode == null ? false : !"null".equalsIgnoreCase(machCode) ? true : false;
        if (byOrder) {
            //根据工单查询装卸单
            PageAndCondition pageAndCondition = new PageAndCondition();
            List<Condition> conditions;
            pageAndCondition.setPagination(new Pagination(1L, 10000000L));
            pageAndCondition.setConditions(Collections.singletonList(new Condition("refWoOrder", "eq", orderId, null)));

            ResponseEntity<Result> orderResult =
                    orderFeignService.selectById(Long.valueOf(orderId));
            Preconditions.checkNotNull(orderResult.getBody(), "查询不到此[%s]工单",
                    orderId);
            WoOrder order = (WoOrder) orderResult.getBody().getData();

            ResponseEntity<Result<IPage<LcmBoxFillDetail>>> lcmBoxFillDetailPage =
                    lcmBoxFillDetailFeignService.page(pageAndCondition);
            pageAndCondition.setPagination(new Pagination(1L, 1L));
            for (LcmBoxFillDetail lcmBoxFillDetail : lcmBoxFillDetailPage.getBody().getData().getRecords()) {
                conditions = new ArrayList<>();
                conditions.add(new Condition("id", "eq", lcmBoxFillDetail.getMasterId(), null));
                conditions.add(new Condition("isCanceled", "eq", 0, null));
                conditions.add(new Condition("isFinished", "eq", 0, null));
                conditions.add(new Condition("isClosed", "eq", 0, null));
                conditions.add(new Condition("docType", "eq", BoxFillDocStatus.INSERT.getStatus(), null));
                pageAndCondition.setConditions(conditions);
                ResponseEntity<Result<IPage<LcmBoxFillMaster>>> lcmBoxFillMasterPage =
                        lcmBoxFillMasterFeignService.page(pageAndCondition);
                if (lcmBoxFillMasterPage.getBody().getData().getRecords().size() != 0) {

                    LcmBoxFillMaster lcmBoxFillMaster =
                            lcmBoxFillMasterPage.getBody().getData().getRecords().stream().findFirst().get();
                    ResponseEntity<Result> boxLedger =
                            lcmBoxLedgerFeignService.getBoxLedger(lcmBoxFillMaster.getBoxId());
                    Preconditions.checkNotNull("查询不到此[%s]框台账信息", lcmBoxFillMaster.getBoxId());
                    LcmBoxLedger lcmBoxLedger = (LcmBoxLedger) boxLedger.getBody().getData();
                    boxInfo = new BoxInfo();
                    if (isHanging) {
                        boxInfo.setBoxCode(lcmBoxFillDetail.getRefBoxCode());
                        boxInfo.setMatCode(order.getMatCode());
                        boxInfo.setDigit(lcmBoxFillDetail.getDigit().longValue());
                        boxInfo.setStandardDigit(lcmBoxLedger.getMatTotalLotDigit().longValue());
                        boxInfo.setWeight(lcmBoxFillDetail.getTheoryWeight());
                        boxInfo.setBoxId(lcmBoxLedger.getId());
                    } else {
                        Long digit = 0L;
                        boxInfo.setStandardDigit(lcmBoxLedger.getMatTotalLotDigit().longValue());
                        digit += lcmBoxFillDetail.getDigit().longValue();
                        boxInfo.setWeight(lcmBoxFillDetail.getTheoryWeight());
                        boxInfo.setMatCode(lcmBoxFillDetail.getMatCode());
                        boxInfo.setBoxId(lcmBoxLedger.getId());
                        boxInfo.setDigit(digit);
                        boxInfo.setBoxCode(lcmBoxLedger.getCode());
                    }
                    boxInfo.setOrder(order.getWoOrder());
                    boxInfo.setOrderId(order.getId());
                    boxInfo.setBoxFillMasterId(lcmBoxFillMaster.getId());

                    getProdBachInfo(answer,order,lcmBoxLedger,boxInfo);
                }
            }
        } else if (byMachCode) {
            getBoxReportedFilterByMachCode(machCode, answer, isHanging);
        }
        return new ListResult(answer);

    }

    private void getBoxReportedFilterByMachCode(String machCode, List<BoxInfo> answer, Boolean isHanging) {
        BoxInfo boxInfo;
        //查询到当前位置修改时间为最新的容器台账
        PageAndCondition pageAndCondition = new PageAndCondition();
        List<Condition> conditions = new ArrayList<>();
        conditions.add(new Condition("location", "eq", machCode, null));
        conditions.add(new Condition("state", "in",
                BoxStatus.INSTALLING.getStatus() + "," + BoxStatus.INSTALLED.getStatus(), null));
        conditions.add(new Condition("modifyTime", "orderBy", "DESC", null));
        pageAndCondition.setPagination(new Pagination(1L, 1L));
        pageAndCondition.setConditions(conditions);
        ResponseEntity<Result> lcmBoxLedgeResult = lcmBoxLedgerFeignService.search(pageAndCondition);
        IPage<LcmBoxLedger> page = (IPage<LcmBoxLedger>) lcmBoxLedgeResult.getBody().getData();
        if (page.getRecords().size() != 0) {
            LcmBoxLedger lcmBoxLedger = page.getRecords().stream().findFirst().get();
            //根据容器台账找到未结束的装单

            conditions = new ArrayList<>();
            conditions.add(new Condition("boxId", "eq", lcmBoxLedger.getId(), null));
            conditions.add(new Condition("isCanceled", "eq", 0, null));
            conditions.add(new Condition("isFinished", "eq", 0, null));
            conditions.add(new Condition("isClosed", "eq", 0, null));
            conditions.add(new Condition("docType", "eq", BoxFillDocStatus.INSERT.getStatus(), null));
            conditions.add(new Condition("modifyTime", "orderBy", "DESC", null));
            pageAndCondition.setConditions(conditions);
            pageAndCondition.setPagination(new Pagination(1L, 1L));
            ResponseEntity<Result<IPage<LcmBoxFillMaster>>> lcmBoxFillMasterPage =
                    lcmBoxFillMasterFeignService.page(pageAndCondition);
            //查询修改工单的装卸主表
            for (LcmBoxFillMaster lcmBoxFillMaster : lcmBoxFillMasterPage.getBody().getData().getRecords()) {
                pageAndCondition.setConditions(Collections.singletonList(new Condition("masterId", "eq",
                        lcmBoxFillMaster.getId(), null)));
                ResponseEntity<Result<IPage<LcmBoxFillDetail>>> lcmBoxFillDetailFeigPage =
                        lcmBoxFillDetailFeignService.page(pageAndCondition);
                if (lcmBoxFillDetailFeigPage.getBody().getData().getRecords().size() == 0) {
                    continue;
                }
                LcmBoxFillDetail lcmBoxFillDetail =
                        lcmBoxFillDetailFeigPage.getBody().getData().getRecords().stream().findFirst().get();

//                conditions = new ArrayList<>();
//                conditions.add(new Condition("isCanceled","eq",0,null));
//                conditions.add(new Condition("isFinished","eq",0,null));
//                conditions.add(new Condition("isClosed","eq",0,null));
//                conditions.add(new Condition("woOrderId", "eq",lcmBoxFillDetail.getRefWoOrder(), null));
//                pageAndCondition.setConditions(Collections.singletonList(new Condition("woOrderId", "eq",lcmBoxFillDetail.getRefWoOrder(), null)));
//
//                ResponseEntity<Result<IPage<WoOrderCompleteDetail>>> orderCompleteDetailPage =
//                        orderCompleteDetailFeignService.page(pageAndCondition);
                WoOrderCompleteDetail orderCompleteByOrder =
                        sfcService.getOrderCompleteByOrder(Long.valueOf(lcmBoxFillDetail.getRefWoOrder()), null);

                //查询工单是否完工
                if (orderCompleteByOrder == null) {
                    pageAndCondition.setPagination(new Pagination(1L, 100000L));
                    pageAndCondition.setConditions(Collections.singletonList(new Condition("refWoOrder", "eq", lcmBoxFillDetail.getRefWoOrder(), null)));

                    ResponseEntity<Result<IPage<LcmBoxFillDetail>>> lcmBoxFillDetailPage =
                            lcmBoxFillDetailFeignService.page(pageAndCondition);
                    for (LcmBoxFillDetail boxFillDetail1 : lcmBoxFillDetailPage.getBody().getData().getRecords()) {
                        pageAndCondition.setPagination(new Pagination(1L, 1L));
                        pageAndCondition.setConditions(Collections.singletonList(new Condition("code", "eq",
                                boxFillDetail1.getRefBoxCode(), null)));
                        ResponseEntity<Result> search = lcmBoxLedgerFeignService.search(pageAndCondition);
                        LcmBoxLedger data =
                                ((IPage<LcmBoxLedger>) search.getBody().getData()).getRecords().stream().findFirst().get();
                        boxInfo = new BoxInfo();
                        if (isHanging) {
                            boxInfo.setBoxCode(boxFillDetail1.getRefBoxCode());
                            boxInfo.setDigit(boxFillDetail1.getDigit().longValue());
                            boxInfo.setWeight(boxFillDetail1.getTheoryWeight());

                            boxInfo.setStandardDigit(data.getMatTotalLotDigit().longValue());
                            boxInfo.setBoxId(data.getId());
                        } else {
                            //一个框多个装框单
                            Long digit = 0L;
                            digit += boxFillDetail1.getDigit().longValue();
                            boxInfo.setWeight(boxFillDetail1.getTheoryWeight());
                            boxInfo.setDigit(digit);
                            //TODO
                            boxInfo.setStandardDigit(data.getMatTotalLotDigit().longValue());
                            boxInfo.setBoxId(data.getId());
                            boxInfo.setBoxCode(data.getCode());

                        }
                        boxInfo.setBoxFillMasterId(boxFillDetail1.getMasterId());
                        ResponseEntity<Result> orderResult =
                                orderFeignService.selectById(Long.valueOf(lcmBoxFillDetail.getRefWoOrder()));
                        Preconditions.checkNotNull(orderResult.getBody().getData(), "查询不到此[%s]工单",
                                lcmBoxFillDetail.getRefWoOrder());
                        WoOrder order = (WoOrder) orderResult.getBody().getData();
                        boxInfo.setOrderId(order.getId());
                        boxInfo.setOrder(order.getWoOrder());
                        boxInfo.setMatCode(order.getMatCode());
                        getProdBachInfo(answer,order,data,boxInfo);
                    }
                } else {
                    return;
                }
            }
        }
    }
    private  void  getProdBachInfo(List<BoxInfo> answer,WoOrder order, LcmBoxLedger lcmBoxLedger,BoxInfo boxInfo){
        PageAndCondition pageAndCondition = new PageAndCondition();
        ResponseEntity<Result> prodBatchResult =
                prodBatchFeignService.selectProdBatchByMatCode(order.getMachineCode(),
                        lcmBoxLedger.getCode(), lcmBoxLedger.getSystemId());
        if (prodBatchResult.getBody().getData() != null) {
            MfgProdBatch mfgProdBatch = (MfgProdBatch) prodBatchResult.getBody().getData();
            boxInfo.setProdBach(mfgProdBatch.getProdBatch());
        }

        pageAndCondition.setConditions(Collections.singletonList(new Condition("woOrder", "eq",
                order.getWoOrder(), null)));
        pageAndCondition.setPagination(new Pagination(1L,1L));
        ResponseEntity<Result<IPage<WoOperationCompleteDetail>>> operationCompleteDetailPage =
                operationCompleteDetailFeignService.page(pageAndCondition);
        if (operationCompleteDetailPage.getBody().getData().getRecords().size() != 0) {
            WoOperationCompleteDetail operationCompleteDetail =
                    operationCompleteDetailPage.getBody().getData().getRecords().stream().findFirst().get();
            operationCompleteDetail.getProdBatch();
            ResponseEntity<Result> byBatch =
                    prodBatchFeignService.getByBatch(operationCompleteDetail.getProdBatch());
            if (byBatch.getBody().getCode() == 0) {
                ArrayList<LinkedHashMap> prodBatchList =
                        (ArrayList<LinkedHashMap>) byBatch.getBody().getData();
                if (prodBatchList.size() == 1) {
                    MfgProdBatch prodBatch =
                            JSON.parseObject(JSON.toJSONString(prodBatchList.stream().findFirst().get()),
                                    MfgProdBatch.class);
                    boxInfo.setMd(prodBatch.getCv4());
                }
            }
        }
        answer.add(boxInfo);

    }


    @Override
    public ObjectResult userBox(Long boxId, Principal principal) {
        ResponseEntity<Result> boxLedgerResult = lcmBoxLedgerFeignService.getBoxLedger(boxId);
        Preconditions.checkNotNull(boxLedgerResult.getBody().getData(), "查询不到此[%s]台账", boxId);
        LcmBoxLedger boxLedger = (LcmBoxLedger) boxLedgerResult.getBody().getData();

        MfgUserEnvironment userEnvironmentByUserCode = sfcService.getUserEnvironmentByUserCode(principal.getName());
        ResponseEntity<Result> prodBatchFeignResult =
                prodBatchFeignService.selectProdBatchByMatCode(userEnvironmentByUserCode.getMachCode(),
                        boxLedger.getCode(), userEnvironmentByUserCode.getSystemId());

        if (prodBatchFeignResult.getBody().getData() != null) {
            MfgProdBatch mfgProdBatch = (MfgProdBatch) prodBatchFeignResult.getBody().getData();
            if (mfgProdBatch.getEnterTime() != null) {
                prodBatchFeignService.update(mfgProdBatch.getProdBatch(), userEnvironmentByUserCode.getSystemId(),
                        principal);
            }
        }
        RtWorkCenter workCenter = sfcService.getWorkCenterByCwocCode(userEnvironmentByUserCode.getCwocCode());
        MfgProdBatch mfgProdBatch = new MfgProdBatch();
        mfgProdBatch.setCwocCode(userEnvironmentByUserCode.getCwocCode());
        mfgProdBatch.setMachCode(userEnvironmentByUserCode.getMachCode());
        mfgProdBatch.setPlantCode(workCenter.getPlantCode());
        mfgProdBatch.setMatCode(boxLedger.getCode());
        mfgProdBatch.setMatName(boxLedger.getName());
        mfgProdBatch.setEnterTime(new Date());
        mfgProdBatch.setEnterCode(principal.getName());
        mfgProdBatch.setEnterName(principal.getName());
        mfgProdBatch.setProcessType(1);
        mfgProdBatch.setMachCode(userEnvironmentByUserCode.getMachCode());
        //TODO 待能查询到公司信息以后再做修改[数据还未确定]
        mfgProdBatch.setCompanyId(requestParams.getCompanyId());
        mfgProdBatch.setSystemId(requestParams.getSystemId());
        mfgProdBatch.setPeriodId(mdmBaseFeignService.getPeriodId().getBody().getData().getId().longValue());
        mfgProdBatch.setFormId(1L);
        ResponseEntity<Result> add = prodBatchFeignService.add(mfgProdBatch);

        ObjectResult objectResult;
        if (add.getBody().getCode() == 0) {
            objectResult = new ObjectResult(((MfgProdBatchResp) add.getBody().getData()).getProdBatch());
        } else {
            objectResult = new ObjectResult("");
            objectResult.modifyCode(Codes.fail.getCode(), Codes.fail.getMsg());
        }
        return objectResult;

    }

    @Override
    public ResponseEntity<ListResult> matStock(String machCode, String type, Long pageIndex, Long pageSize) {
        List<MatStock> answer = new ArrayList<>();
        PageAndCondition pageAndCondition = new PageAndCondition();
        pageAndCondition.setPagination(new Pagination(1L, 1000000L));
        List<Condition> conditions ;
        MatStock matStock;
        if ("md".equalsIgnoreCase(type)) {
            //获取当前机台不同的模具型号
            ResponseEntity<ListResult<String>> mdCvByMachCode = mfgMdApplyUseFeignService.getMdCvByMachCode(machCode,
                    pageIndex, pageSize);
            if (mdCvByMachCode.getBody().getData().size() != 0) {
                for (String mdCv : mdCvByMachCode.getBody().getData()) {
                    matStock = new MatStock();
                    conditions = new ArrayList<>();
                    conditions.add(new Condition("matCodeCv", "eq", mdCv, null));
                    conditions.add(new Condition("machCode", "eq", machCode, null));
                    pageAndCondition.setConditions(conditions);
                    ResponseEntity<Result<IPage<MfgMdApplyUse>>> mfgMdApplyUseResult =
                            mfgMdApplyUseFeignService.selectDataByConditionPage(pageAndCondition);
                    if (mfgMdApplyUseResult.getBody().getData().getRecords().size() != 0) {
                        //TODO 待修改真实数据
                        matStock.setMatCodeCv(mdCv);
                        matStock.setMdApplyUseList(mfgMdApplyUseResult.getBody().getData().getRecords());
                        MfgMdApplyUse mfgMdApplyUse =
                                mfgMdApplyUseResult.getBody().getData().getRecords().stream().findFirst().get();
                        //模孔
                        matStock.setCv1(mfgMdApplyUse.getCv5());
                        //模径
                        matStock.setCv2(mfgMdApplyUse.getCv1());
                        //模重
                        matStock.setCv3(mfgMdApplyUse.getCv5Name());
                        //模具数量
                        matStock.setCv4(String.valueOf(mfgMdApplyUseResult.getBody().getData().getTotal()));
                        answer.add(matStock);
                    }
                }
            }
        } else if ("as".equalsIgnoreCase(type)) {
            ResponseEntity<ListResult<String>> lotCodeByMachCode =
                    mfgConsumeDetailFeignService.getLotCodeByMachCode(machCode, pageIndex, pageSize);
            for (String lotCode : lotCodeByMachCode.getBody().getData()) {
                List<MfgConsume> mfgConsumes = new ArrayList<>();
                pageAndCondition.setConditions(Collections.singletonList(new Condition("lotCode", "eq", lotCode,
                        null)));
                ResponseEntity<Result<IPage<MfgConsumeDetail>>> mfgConsumeDetailResult =
                        mfgConsumeDetailFeignService.selectDataByConditionPage(pageAndCondition);

                List<MfgConsumeDetail> records = mfgConsumeDetailResult.getBody().getData().getRecords();
                Iterator<MfgConsumeDetail> iterator = records.iterator();
                MfgConsumeMaster consumeMaster = null;
                MfgConsume mfgConsume ;
                while (iterator.hasNext()) {
                    MfgConsumeDetail consumeDetail = iterator.next();
                    ResponseEntity<Result<MfgConsumeMaster>> byId =
                            mfgConsumeMasterFeignService.getById(consumeDetail.getMasterId().toString());
                    Preconditions.checkNotNull(byId.getBody().getData(), "查询不到此【%s】物料的物料消耗信息",
                            consumeDetail.getMasterId());
                    consumeMaster = byId.getBody().getData();
                    if (consumeMaster.getDocStatus() == 120 || !(consumeMaster.getMachCode().equalsIgnoreCase(machCode))) {
                        iterator.remove();
                        continue;
                    }
                    mfgConsume = new MfgConsume();
                    mfgConsume.setMfgConsumeMaster(consumeMaster);
                    mfgConsume.setMfgConsumeDetail(consumeDetail);
                    mfgConsumes.add(mfgConsume);
                }

                if (records.size() != 0) {
                    matStock = new MatStock();
                    MfgConsumeDetail consumeDetail = records.stream().findFirst().get();
                    matStock.setMfgConsumeDetailList(mfgConsumes);
                    //炉次号
                    matStock.setCv1(consumeDetail.getDocItem().toString());

                    if (consumeMaster != null) {
                        //预计棒温
                        matStock.setCv2(consumeMaster.getUserTxt1());
                        //上机时间
                        matStock.setCv3(consumeMaster.getUserTxt1Name());
                    }
                    matStock.setMatCodeCv(lotCode);
                    //加温时长
                    matStock.setCv4("1.5");
                    answer.add(matStock);
                }
            }
        }
        return ResponseEntity.ok(new ListResult(answer));
    }

    @Override
    public ResponseEntity<ObjectResult> cancelUseBox(Long boxFillMasterId, Principal principal) {
        LcmBoxFillMaster lcmBoxFillMaster = new LcmBoxFillMaster();
        lcmBoxFillMaster.setId(boxFillMasterId);

        return ResponseEntity.ok(ConverterUtil.ConverterByResult(lcmBoxFillFeignService.updateDocStatus(DocOperation.CANCEL.getType(),
                lcmBoxFillMaster).getBody()));
    }

    @Override
    public ResponseEntity<ListResult> warmingMatFilter(String machCode, Long pageIndex, Long pageSize, String type) {
        PageAndCondition pageAndCondition = new PageAndCondition();
        List<Condition> conditions = new ArrayList<>();

        pageAndCondition.setPagination(new Pagination(pageIndex, pageSize));

        conditions.add(new Condition("machCode", "eq", machCode, null));
        conditions.add(new Condition("isClosed", "nn", 0, null));
        conditions.add(new Condition("isCanceled", "nn", 0, null));
        conditions.add(new Condition("docStatus", "eq", MatStatus.processing.getStatus(), null));

        ListResult result = null;
        MfgConsume consume = null;
        if (MatType.模具.equalsIgnoreCase(type)) {
            conditions.add(new Condition("cv4", "orderBy", "DESC", null));
            conditions.add(new Condition("isFinished", "nn", 0, null));
            pageAndCondition.setConditions(conditions);
            ResponseEntity<Result<IPage<MfgMdApplyUse>>> mfgMdApplyUseResource =
                    mfgMdApplyUseFeignService.selectDataByConditionPage(pageAndCondition);
            result = new ListResult(mfgMdApplyUseResource.getBody().getData().getRecords());
            result.setMsg(mfgMdApplyUseResource.getBody().getData().getTotal().toString());
        } else if (MatType.铝棒.equalsIgnoreCase(type)) {
            conditions.add(new Condition("userTxt1Name", "orderBy", "DESC", null));
            pageAndCondition.setConditions(conditions);
            List<MfgConsume> answer = new ArrayList<>();
            ResponseEntity<Result<IPage<MfgConsumeMaster>>> mfgConsumeMasterResult =
                    mfgConsumeMasterFeignService.selectDataByConditionPage(pageAndCondition);
            for (MfgConsumeMaster consumeMaster : mfgConsumeMasterResult.getBody().getData().getRecords()) {

                pageAndCondition.setPagination(new Pagination(1L, 1L));
                pageAndCondition.setConditions(Collections.singletonList(new Condition("masterId", "eq",
                        consumeMaster.getId(), null)));
                ResponseEntity<Result<IPage<MfgConsumeDetail>>> mfgConsumeDetailResult =
                        mfgConsumeDetailFeignService.selectDataByConditionPage(pageAndCondition);

                if (mfgConsumeDetailResult.getBody().getData().getRecords().size() != 0) {
                    consume = new MfgConsume();
                    consume.setMfgConsumeDetail(mfgConsumeDetailResult.getBody().getData().getRecords().stream().findFirst().get());
                    consume.setMfgConsumeMaster(consumeMaster);
                    answer.add(consume);
                }
            }

            result = new ListResult(answer);
        }

        return ResponseEntity.ok(result);
    }




}
